package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {

    @Reference
    OrdersettingService ordersettingService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            // 从上传组件中读取文件，利用POI工具解析内容
            List<String[]> stringList = POIUtils.readExcel(excelFile);

            //把stringList数据转换到List<OrderSetting>中
            List<OrderSetting> orderSettingList = new ArrayList<>();

            for (String[] strings : stringList) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(new SimpleDateFormat("yyyy/MM/dd").parse(strings[0]));
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                //添加到集合中
                orderSettingList.add(orderSetting);
            }
            //更新数据库
            ordersettingService.editOrderSettingList(orderSettingList);
            return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/fingByMonth")
    public Result fingByMonth(String month){
        try {
            List<OrderSetting> orderSettingList = ordersettingService.fingByMonth(month);
            //把搜索到的数据转换成页面需要的数据
            List<Map<String,Object>> mapList = new ArrayList<>();
            for (OrderSetting orderSetting : orderSettingList) {
                //一个OrderSetting对应一个map集合
                Map<String,Object> map = new HashMap<>();
    //            map.put("date",orderSetting.getOrderDate().getDate());
                map.put("date",new SimpleDateFormat("dd").format(orderSetting.getOrderDate()));
                map.put("number",orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());
                //把map添加到集合中
                mapList.add(map);

            }
            return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }

    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody OrderSetting orderSetting){
        try {
            List<OrderSetting> orderSettingList = new ArrayList<>();
            orderSettingList.add(orderSetting);
            ordersettingService.editOrderSettingList(orderSettingList);
            return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ORDERSETTING_FAIL);
        }

    }

}
